const db = require('../db/index')

// 获取人员
exports.kunlist = (req, res) => {
    const sql = 'select * from kun '
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('未查询到坤派人员数据')
        res.send({
            code: 200,
            message: '成功',
            data: results
        })
    })
}
// 新增接口
exports.addKun = (req, res) => {
    const sql = 'INSERT INTO kun (kunName, ikun_year, ikun_position,ikun_pet_phrase) VALUES(?,?,?,?)'
    const { kunName, ikun_year, ikun_position, inkun_pet_phrase } = req.body
    db.query(sql, [kunName, ikun_year, ikun_position, inkun_pet_phrase], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('新增坤员失败')
        res.send({
            code: 200,
            message: '新增成功',
        })
    })
}
// 删除方法
exports.remove = (req, res) => {
    const deleteSql = 'delete from kun where id =?'
    console.log(req.query.id);
    db.query(deleteSql, [req.query.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('删除失败')
        res.send({
            code: '200',
            message: '删除成功',
        })
    })
}

// 更新方法
exports.upDate = (req, res) => {
    const upDateSql = 'update kun set kunName=?,ikun_year=?, ikun_position=?,ikun_pet_phrase=?  where id =?'
    const { kunName, ikun_year, ikun_position, ikun_pet_phrase, id } = req.body
    db.query(upDateSql, [kunName, ikun_year, ikun_position, ikun_pet_phrase, id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('更新失败')
        res.send({
            code: '200',
            message: '更新成功',
        })
    })
}

// 查询单条数据
exports.getUser = (req, res) => {
    const getUserSql = 'select * from kun where id =?'
    db.query(getUserSql, [req.query.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('查询失败')
        res.send({
            code: '200',
            message: '查询成功',
            data: results[0]
        })
    })
}
